#!/bin/bash

#-------------------------------------------------------------------------------
# hscCopyToDVD.sh
#
# This shell script is invoked to copy a file to a DVD cartridge.
#
# Usage: hscCopyToDVD <fullyQualifiedFileNameOnFixedDisk> <filenameOnDVD>
#
# Return Codes:
# 0 - Copy was successful
# 1 - Error mounting the DVD. Possible problems are that there
#     is no cartridge inserted or the cartridge is write protected or
#     the mount point was not found in /etc/fstab
# 2 - The copy was successful but the DVD was not successfully
#     unmounted after the copy was attempted.
# 3 - The copy was not successful and the DVD was successfully
#     unmounted after the copy was attempted.
# 4 - The copy was not successful and the DVD was not successfully
#     unmounted after the copy was attempted.
# 5 - The DVD is already mounted.
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# The mount point for the DVD.
# A corresponding device entry should exist for this mount point in /etc/fstab
#-------------------------------------------------------------------------------
DVD_MOUNTPOINT=/mnt/cdrom

# Mount the DVD and check for write-protect.  This might not work for the
# DVD-RAM media, but this check won't hurt anything.
# Note, the mount command fails if no DVD cartridge in drive or if
# the medium is write protected.
mount -wv $DVD_MOUNTPOINT 
if [ $? -eq 0 ] ; then
    # The mount command was successful and the cartridge is not
    # write protected.

    # Go to target directory
    cd $DVD_MOUNTPOINT

    # Copy the data to DVD - assume it's been formatted
    cp -f ${1} $DVD_MOUNTPOINT/${2}
    if [ $? -eq 0 ] ; then
        # The file was copied successfully.
	cd /
        umount $DVD_MOUNTPOINT
        if [ $? -eq 0 ] ; then
            # The DVD cartridge was successfully unmounted.
            exit 0
        else
            # The DVD cartridge was not successfully unmounted.
	    exit 2
        fi
    else
        # The file was not copied successfully.
	cd /
        umount $DVD_MOUNTPOINT
        if [ $? -eq 0 ] ; then
            # The DVD cartridge was successfully unmounted.
            exit 3
        else
            # The DVD cartridge was not successfully unmounted.
	    exit 4
        fi
    fi
else
    # The mount command failed.
    grep $DVD_MOUNTPOINT /etc/mtab
    if [ $? -eq 0 ] ; then
        # The DVD is already mounted.
	exit 5
    else
        # The DVD is not already mounted.
	exit 1
    fi
fi
